<?php

namespace Common\Lib\Payments\WeixinApp;

use Common\Lib\Payments\PaymentPlugin;

/**
 * @class alipay
 * @brief 微信支付
 */
class WeixinApp extends PaymentPlugin
{

    public $name = '微信支付';
    protected $Api;

    public function __construct($payment, $backParam = array())
    {
        parent::__construct($payment, $backParam);
        $this->serverCallbackUrl = U('/payment/callback', array_merge($backParam, array('_id' => $this->paymentId, '_notify' => 1)), false, true) . '/';
        include_once("WxPayPubHelper/WxPay.Api.php");
        include_once("WxPayPubHelper/WxPay.Data.php");
        $this->initConfig($payment);
        $this->Api = new \WxPayUnifiedOrder();
    }

    public function initConfig()
    {
        \WxPayConfig::$APPID = $this->config('appid');
        \WxPayConfig::$MCHID = $this->config('mch_id');
        \WxPayConfig::$KEY = $this->config('key');
        \WxPayConfig::$NOTIFY_URL = $this->serverCallbackUrl;
    }

    public function notifyStop()
    {
        echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        exit;
    }

    public function getSubmitUrl()
    {

    }

    public function getSendData($payment)
    {
        $unifiedOrder = new \WxPayUnifiedOrder();
        $unifiedOrder->SetBody($payment['R_Name'] ? $payment['R_Name'] : "在线充值"); //商品或支付单简要描述
        $unifiedOrder->SetOut_trade_no($payment['M_OrderNO']);
        $unifiedOrder->SetTotal_fee($payment['M_Amount'] * 100);
        $unifiedOrder->SetTrade_type("APP");
        $unifiedOrder->SetNotify_url($this->serverCallbackUrl);
        $result = \WxPayApi::unifiedOrder($unifiedOrder);

        if (is_array($result)) {
            echo json_encode($result);
        }
        exit;
    }

    public function getOpenId()
    {
        if ($_GET['code']) {
            $code = $_GET['code'];
            $this->Api->setCode($code);
            return $this->Api->getOpenId();
        } else {
            $url = $this->Api->createOauthUrlForCode(\WxPayConf_pub::$JS_API_CALL_URL);
            Header("Location: $url");
            return;
        }
    }

    public function serverCallbackUrl($callbackData)
    {
        $this->isNotify = true;
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];

        $reply = $this->reply();
        try {
            $result = \WxPayResults::Init($xml);
            $reply->orderNo = $result['out_trade_no'];
            $reply->message = '支付成功';
            $reply->money = $result['total_fee'] / 100;
            $reply->setStatus(true);
        } catch (WxPayException $e) {
            $msg = $e->errorMessage();
            $reply->message = $msg;
            $reply->setStatus(false);
        }
        file_put_contents('./a.txt', var_export($reply, true));
        return $reply;

    }

    public function callback($callbackData)
    {
        return $this->serverCallbackUrl($callbackData);
    }

    public function notifyCallback($callbackData)
    {
        return $this->serverCallbackUrl($callbackData);
    }

    /**
     * @brief 返回配置参数
     */
    public function configParam()
    {
        return array(
            'channel' => 'APP端标识',
            'desc' => 'APP端描述',
            'appid' => '公众账号ID',
            'mch_id' => '商户号',
            'key' => 'API密钥',
        );
    }

}
